package inf.coach.mobile;

import android.app.Activity;
import android.app.AlertDialog;
import android.app.AlertDialog.Builder;
import android.content.DialogInterface;
import android.media.MediaPlayer;
import android.media.MediaPlayer.OnCompletionListener;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;

public class CommentAudio extends Activity {

	public static long comment_id;
	public static String filename="";
	final String url="http://141.76.84.22/AnnotationFiles/audio/";
	private MediaPlayer mediaPlayer;
	
	public void onCreate(Bundle savedInstanceState) {
		
        super.onCreate(savedInstanceState);
        setContentView(R.layout.commentaudio);
        setTitle("Audio Komentar informationen");
        Bundle bundle = this.getIntent().getExtras();      
        comment_id = bundle.getLong("comment_id");
   	 
        final Button play = (Button) findViewById(R.id.button_spielen);
        final Button stop=(Button) findViewById(R.id.button_audio_stop);
        stop.setEnabled(false);
        play.setEnabled(false);
        
   	 if(ConnectionReceiver.isInternetConnected(getApplicationContext()))
   		{
   				filename = webservice.getCommentAudio(comment_id);
   				filename=filename.replaceAll(".amr", ".mp3");
   				System.out.println(filename);
   		}	
   	else{
   		message.show(CommentAudio.this,"Fehler", "Keine Internetverbindung !");
   					//mTts.speak("Information Window, Error, There is no Internet Connection , Ok button", TextToSpeech.QUEUE_FLUSH, null);
   		}
   	 
   	try{
		//实际音频格式为amr,但是http post 不支持 amr,所以把文件名改成.mp3格式
		System.out.println(url+filename);	
	mediaPlayer= new MediaPlayer();
	mediaPlayer.setDataSource(url+filename);
	// 准备并且启动播放器
	mediaPlayer.prepare();
    play.setEnabled(true);
	
   	}
	 catch (Exception e) {
		 message.show(CommentAudio.this,"Fehler", "Audio Daten verloren !");
	 }

        
        play.setOnClickListener(new Button.OnClickListener() {

        
			public void onClick(View arg0) {
				
					// 为播放器设置数据文件 
					stop.setEnabled(true);
	        		play.setEnabled(false);
					mediaPlayer.start();
					mediaPlayer.setOnCompletionListener(new OnCompletionListener() {
					@Override
					public void onCompletion(MediaPlayer mp) {
						Toast.makeText(CommentAudio.this, "Die Wiedergabe ist abgeschlossen", Toast.LENGTH_LONG).show();
						stop.setEnabled(false);
		        		play.setEnabled(true);
					}
					});
					
					}
        
        });
        
        stop.setOnClickListener(new Button.OnClickListener() {


			public void onClick(View arg0) {
				
	            if(mediaPlayer!=null)
	            {
	            	mediaPlayer.stop();
	            }
        		
        		System.out.println("stop");
        		play.setEnabled(true);
        		stop.setEnabled(false);
			}
        });
        
	}
	
    protected void dialoggoback() { 
        AlertDialog.Builder builder = new Builder(CommentAudio.this); 
        builder.setMessage("Möchten Sie zur letzten Seite zurück springen?"); 
        builder.setTitle("Information"); 
        builder.setPositiveButton("ok", 
                new android.content.DialogInterface.OnClickListener() { 
                    @Override 
                    public void onClick(DialogInterface dialog, int which) { 
                        dialog.dismiss(); 
                        CommentAudio.this.finish(); 
                    }

                }); 
        builder.setNegativeButton("abbrechen", 
                new android.content.DialogInterface.OnClickListener() { 
                    @Override 
                    public void onClick(DialogInterface dialog, int which) { 
                        dialog.dismiss(); 
                    } 
                }); 
        builder.create().show(); 
    } 
    @Override 
    public boolean onKeyDown(int keyCode, KeyEvent event) { 
        if (keyCode == KeyEvent.KEYCODE_BACK && event.getRepeatCount() == 0) { 
            dialoggoback(); 
            return false; 
        } 
        return false; 
    } 
}
